﻿#include "socketthread.h"

SocketThread::SocketThread(QTcpSocket *socket):QThread()
{
    bRun=true;
    this->socket=socket;
    connect(socket,SIGNAL(disconnected()),this,SLOT(socketDisconnected()));

    buffer[0]=0x30;
    bufIdx=1;
}

void SocketThread::run()
{
    myDebug<<"开始socket线程";

    while(bRun)
    {
        if(socket->waitForReadyRead(200))
        {
            QByteArray buf=socket->readAll();
            myDebug<<"recv:"<<buf.toHex();

            memcpy(buffer+bufIdx,buf.data(),buf.length());
            bufIdx += buf.length();

            process();
        }
    }

    socket->close();
    myDebug<<"结束socket线程";
}

void SocketThread::socketDisconnected()
{
    bRun=false;
    myDebug<<"disconnected()";
}

void SocketThread::process()
{
    socket->write(buffer,bufIdx);
}
